背景
主要記錄一些使用jenkin pipeline 部署 不同環境例如:dev,uat 時的一些知識
01 大致流程
主要寫三個stages
- building stage
- deploy stage
- health check stage
02 WaitUntil
指令
timeout(5) {
waitUntil {
def r = sh script: "wget -q ${host} -O /healthCheck", returnStatus: true
return r == 0
}
}
waitUntil
是一個Jenkin Pipeline 的command ,會不斷執行body内的動作,直到return true , 搭配timeout 使用可以用來檢查系統是否有成功部署.
The
waitUntil
command in Jenkins Pipeline is used to create a loop that will continue to execute its body until the body returnstrue
. It's often used when you need to wait for a certain condition to be met before proceeding with the rest of the pipeline.In the provided script, the
waitUntil
command is used to repeatedly execute awget
command until it succeeds. Thewget
command is used to download a file from a specific URL, and it returns an exit status of 0 if it succeeds and a non-zero exit status if it fails.The
sh
step is used to execute thewget
command, and thereturnStatus: true
option is used to make thesh
step return the exit status of the command. The exit status is stored in a variable namedr
.The
return r == 0
line is the condition that thewaitUntil
command is waiting for. If thewget
command succeeds andr
is 0, this line will returntrue
and thewaitUntil
command will stop looping. If thewget
command fails andr
is not 0, this line will returnfalse
and thewaitUntil
command will continue to loop.The
timeout(5)
command is used to limit the amount of time that thewaitUntil
command can loop for. If thewaitUntil
command is still looping after 5 minutes, thetimeout
command will cause the pipeline to fail.
03 SSH 相關指令
sshCommand remote: remote (map) , sudo:boolean , command: """ """
sshCommand 是 Jenkins Pipeline 的一個步驟,用於在遠程服務器上執行命令。這個步驟接受一些參數來配置其行為。
在 sshCommand remote: remote, sudo: false, command: """ """ 中:
remote: remote 是一個參數,指定了要在哪個遠程服務器上執行命令。remote 是一個 map,通常包含如主機名、用戶名和密碼等信息。
sudo: false 是一個參數,指定是否以超級用戶權限運行命令。如果設置為 true,則命令將以超級用戶權限運行。如果設置為 false,則命令將以普通用戶權限運行。
command: """ """ 是一個參數,指定了要執行的命令。命令應該被包含在三個引號之間,這樣就可以包含多行文本。
sshPut
和 sshGet
是 Jenkins Pipeline 的步驟,用於在遠程服務器上上傳和下載文件。
sshPut
的基本語法如下:
sshPut remote: <remote>, from: <localPath>, into: <remotePath>
remote: <remote>
指定了要上傳文件的遠程服務器。<remote>
是一個 map,通常包含如主機名、用戶名和密碼等信息。from: <localPath>
指定了要上傳的本地文件或目錄的路徑。into: <remotePath>
指定了遠程服務器上的目標路徑。
sshGet
的基本語法如下:
sshGet remote: <remote>, from: <remotePath>, into: <localPath>
remote: <remote>
指定了要從哪個遠程服務器下載文件。<remote>
是一個 map,通常包含如主機名、用戶名和密碼等信息。from: <remotePath>
指定了要下載的遠程文件或目錄的路徑。into: <localPath>
指定了本地的目標路徑。
04 Jekins Pipeline 的 stage
, step
和script
在 Jenkins Pipeline 中,stage
, steps
, 和 script
是用來組織和控制流程的關鍵結構。
stage
: 一個stage
是 Pipeline 中的一個階段,通常用於表示 CI/CD 流程中的一個重要階段。例如,你可能有一個名為 "Build" 的stage
,一個名為 "Test" 的stage
,和一個名為 "Deploy" 的stage
。每個stage
都會在 Jenkins 的 UI 中顯示,使得流程的進度和結果一目了然。steps
:steps
是在stage
中執行的操作。每個step
是一個單獨的任務,可以是一個簡單的命令,也可以是一個複雜的腳本或者是一個外部插件的功能。例如,一個 "Build"stage
可能包含了 "Checkout code"、"Compile code"、"Run unit tests" 等steps
。script
:script
是一個特殊的step
,用於執行 Groovy 腳本。在script
區塊中,你可以使用更複雜的邏輯,例如條件語句、迴圈等。這對於需要更複雜控制流程的情況非常有用。
總的來說,stage
、steps
和 script
是用來組織和控制 Jenkins Pipeline 的工具。stage
是流程的大階段,steps
是每個階段中的具體操作,而 script
則提供了更複雜的控制流程的能力。